<?php
class eMode {
	private $_table;
	
	private $_instance = array();
	
	private $_cache;
	
	private $_conf = "dev";
	
	private $_dbconfiglist;

	
	public function setTable($table) {
		$this->_table = $table;
		return $this;
	}
	
	public function cache($flag = false) {
		$this->_cache = $flag;
		return $this;
	}
	
	public function setDBConfList($dbconfiglist) {
		$this->_dbconfiglist = $dbconfiglist;
		return $this;
	}
	
	public function setDbConf($conf) {
		$this->_conf = $conf;
	}
	
	public function desconnect() {
		foreach ($this->_instance as $v) {
			$v->disconnect();
		}
	}
	/**
	 * 
	 * @return epdo
	 */
	protected function db() {
		if (!isset($this->_instance[$this->_conf])) {
			$db  = new epdo();
			$db->setDb($this->_dbconfiglist[$this->_conf]);
			$this->_instance[$this->_conf] = $db;
		}
		return $this->_instance[$this->_conf];
	}
	
	private function setcache($key, $value, $ParamValue = null) {
		
	
	}
	
	private function getCache($key, $ParamValue = null) {
		
	}
	
	private function fetchAll($sql, $ParamValue = null) {
		if ($this->_cache) {
			$result = $this->getCache($sql, $ParamValue);
		}
		if (!$result) {
			$result = $this->db()->fetchAll($sql, $ParamValue);
			$result && $this->_cache  && $this->setcache($sql, $result, $ParamValue);
		}
		return $result;
	}
	
	private function fetchRow($sql, $ParamValue = null) {
		$result = $this->db()->fetchAll($sql, $ParamValue);
		$this->_cache && $this->setcache($sql, $result, $ParamValue);
		return $result;
	}
	
	/**
	 * 生成pdo操作参数
	 * @param array $sqlParam
	 */
	protected function buldParam($sqlParam) {
		$paramStr = array();
		$paramValue = array();
	
		foreach ($sqlParam as $key => $v) {
			$keyParamStr =  ":{$key}";
			array_push($paramStr, $keyParamStr);
			$paramValue[$keyParamStr] = $v;
		}
	
		return array($paramStr, $paramValue);
	}
	/**
	 * 执行插入操作的方法
	 * @param $table : 表名
	 * @param $values : 数组数据
	 * @return id 插入的id
	 */
	public function insert($values) {
		$tableParam = array(":table" => $this->_table);
		$insertKeys = array_keys($values);
		list($paramStr, $ParamValue) = $this->buldParam($values);
	
		//	$ParamValue = array_merge($tableParam, $ParamValue);
	
		$sql = "INSERT INTO {$this->_table} (".implode(",", $insertKeys).") VALUES(".implode(",", $paramStr).")";
		//echo $sql."<BR>";
		//print_r($ParamValue);
		$db = $this->db();
		$db->query($sql, $ParamValue);
		return $db->lastInsertId();
	}
	
	/**
	 * 执行删除操作的方法
	 * @param $table : 表名
	 * @param $where : 删除条件
	 * @return AffectRow 影响的行数
	 */
	function delete($where = "", $param=null) {
	
		$sql = "DELETE FROM {$this->_table} ";
		if ($where !== "")
			$sql .= " WHERE {$where}";
		$db = $this->db();
		//echo $sql."<BR>";
	
		return $db->query($sql, $param);
	}
	
	/**
	 * 执行更新操作的方法
	 * @param $table : 表名
	 * @param $values : 数组数据
	 * @param $where ： 更新条件
	 */
	function update($values, $where, $param = array()) {
		$table = $this->_table;
		$sql = "UPDATE {$table} SET ";
		$endprefix = "";
	
		foreach ( $values as $k => $v ) {
				
			$endprefix .= (empty($endprefix) ? "" : ",")."$k=:$k";
			$param[":$k"] = $v;
		}
		$sql .= $endprefix;
		$sql .= " WHERE {$where}";
		return $this->db()->query($sql, $param);
	}
	
	/**
	 * 执行查询操作
	 * @param $what : 查询字段
	 * @param $where : 查询条件
	 * @param $orderby : 查询排序
	 * @param $limit : 查询数量
	 */
	function select($what, $where = '', $orderby = '', $limit = '', $param = null) {
	
		$table = $this->_table;
		$sql = "SELECT {$what} FROM {$table}";
		if ($where != '')
			$sql .= " WHERE {$where}";
		if ($orderby != '')
			$sql .= " ORDER BY {$orderby}";
		if ($limit != '')
			$sql .= " LIMIT {$limit}";
		//echo $sql;
		return $this->fetchAll($sql, $param);
	}
	
	
	function selectOne($what, $where = '', $orderby = '', $limit = '', $param = null) {
	
		$table = $this->_table;
		$sql = "SELECT {$what} FROM {$table}";
		if ($where != '')
			$sql .= " WHERE {$where}";
		if ($orderby != '')
			$sql .= " ORDER BY {$orderby}";
		if ($limit != '')
			$sql .= " LIMIT {$limit}";
		//echo $sql."<BR>";
		//print_r($param);
		return $this->fetchRow($sql, $param);
	}
}

?>